import React, { createContext, useReducer } from 'react';

// 创建action的type类型，这个应该单独创建一个文件夹
export const UPDATE_COLOR = 'UPDATE_COLOR';

// 根据条件修改state
const reducer = (state, action) => {
    if (action.type === UPDATE_COLOR) {
        return action.color
    } else {
        return state
    }
}
// 创建Context用来给子组件获取传下去的东西
export const CreateContext = createContext({})

export const Color = (props) => {
    const [color, dispatch] = useReducer(reducer, 'blue')
    return (
        // 把useReducer的内容和触发方法传下去给子组件获取，修改useReducer里面的数据
        <CreateContext.Provider value={{ color, dispatch }}>
            {props.children}
        </CreateContext.Provider>
    )
}

